<!--
 * @Author: wangming
 * @Date: 2021-05-27 17:52:33
 * @LastEditors: wangming
 * @LastEditTime: 2022-05-13 10:19:12
 * @Description: 业务模型
-->
<template>
  <!--  todo model-->
  <el-form-item :label="formlabel">
    <el-tooltip
      v-if="value.tagName == 'hlTree'"
      class="item"
      effect="dark"
      content="该组件只能绑定树形结构的业务模型"
      placement="top"
    >
      <i class="el-icon-question f14"></i>
    </el-tooltip>
    <tree-select
      v-model="value.__config.modelCode"
      class="mb10"
      placeholder="请选择业务模型"
      filterable
      :fieldMap="{
        value: 'code',
        label: 'name',
        disabled: 'disabled',
      }"
      :filterNodeMethod="filterNodeMethod"
      :treeData="list"
      @change="changeModel"
    />
  </el-form-item>
</template>
<script>
import basicmixins from "./basicmixins";
import { getHmmodelfolderListTree } from "@/apis/paas/HmModelFolderController.js";
import { filterNodeMethod } from "@/utils/treeSelectFormat";
import { cloneDeep } from "lodash";

export default {
  props: {
    formlabel: {
      type: String,
      default: "绑定业务模型",
    },
  },
  async mounted() {
    var data = await getHmmodelfolderListTree();
    data.forEach((node) => {
      node.type = "folder";
    });
    this.list = this.createModelTreeNode(data);
    if (!this.value.__config.modelCode) {
      if (
        (this.value.tagName == "hlTree" ||
          this.value.redirectName == "hlEcharts") &&
        this.list.length > 0
      ) {
        // 取业务模型第一个树结构
        const modelCode = this.list[0].children[0]?.code;
        if (modelCode) {
          this.$set(this.value.__config, "modelCode", modelCode);
          this.changeModel(modelCode);
        }
      } else if (this.modelCode) {
        this.$set(this.value.__config, "modelCode", this.modelCode);
        this.changeModel(this.modelCode);
        this.checkIsTable(this.modelCode);
      }
    } else {
      this.checkIsTable(this.value.__config.modelCode);
    }
  },
  computed: {
    modelCode() {
      return this.$route.query.modelCode;
    },
  },
  mixins: [basicmixins],
  data() {
    return {
      filterNodeMethod,
      list: [],
    };
  },
  methods: {
    changeModel(modelCode) {
      this.list.forEach((folder) => {
        const children = folder.modelResults || [];
        children.forEach((item) => {
          if (item.code === modelCode) {
            this.value.__config.moduleId = item.id;
          }
        });
      });
      this.checkIsTable(modelCode);
    },
    createModelTreeNode(tree) {
      return cloneDeep(tree).map((node) => {
        node.disabled = node.type === "folder";
        if (node.modelResults) {
          node.id = "folder" + node.id;
          node.children = node.modelResults || [];
          node.children.forEach((item) => {
            item.name = `${item.name}(${
              item.supportProcess ? "流程模型" : "表单模型"
            })(${item.code})`;
          });
          if (this.value.redirectName == "hlEcharts") {
            node.children = node.children.filter(
              (item) => item.type == "VIRTUAL"
            );
          }
          if (this.value.tagName == "hlTree") {
            node.children = node.children.filter((item) => item.supportTree);
          }
        }
        return node;
      });
    },
    checkIsTable(modelCode) {
      this.list.map((node) => {
        if (node.modelResults) {
          node.children.forEach((item) => {
            if (item.code === modelCode) {
              if (
                this.value.tagName === "hlTableList" ||
                this.value.tagName === "hlTableForRelation"
              ) {
                this.$set(this.value.__config, "supportTree", item.supportTree);
              }
            }
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped></style>
